<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>reflect</title>
</head>

<body>



  <script>

    const obj = {
      name: 'jack',
      age: 18,
      sayHi() {
        return `${obj.name}===${obj.age}`
      }
    }

    console.log("Reflect --->", Reflect)

    // get
    const _name = Reflect.get(obj, 'name')
    console.log("_name --->", _name)

    // has
    const _isAttribute = Reflect.has(obj, 'n')
    console.log("_isAttribute --->", _isAttribute)

    // set
    const _isSuc = Reflect.set(obj, 'gander', 'male')
    console.log("_isSuc --->", _isSuc)

    console.log("obj --->", obj)

    console.log('n' in obj)

    console.log('%c <-----------segmentation------------>', 'color: red');

    const obj2 = {}

    const __obj2__ = new Proxy(obj2, {
      get(a, b, c) {
        console.log("c === __obj2__ --->", c === __obj2__)
        console.log("a === obj2 --->", a === obj2)
        console.log("a,b,c --->", a, b, c)
        if (a[b]) {
          return Reflect.get(a, b)
        } else {
          return { msg: `该对象没有 ${b} 属性`, status: 404 }
        }
      }
    })

    console.log("Reflect.get() --->", Reflect.get(__obj2__, 'name'))

    console.log('%c <-----------segmentation------------>', 'color: red');



    class _Reflect {

      constructor() {
        this.isReflect = this.isReflectFun()
      }

      get(target, key) {
        if (this.isReflect) {
          return Reflect.get(target, key)
        } else {
          if (key in target) {
            return target[key]
          } else {
            return undefined
          }
        }
      }

      set(target, key, value) {
        if (this.isReflect) {
          return Reflect.set(target, key, value)
        } else {
          target[key] = value
          if (key in target)
            return true
          else
            return false
        }
      }

      isReflectFun() {
        return window.hasOwnProperty('Reflect')
      }

    }

    const reflect = new _Reflect()

    const __obj__ = {}

    console.log("__obj__ --->", __obj__)

    const name = reflect.get(__obj__, 'name')
    console.log("name --->", name)

    const _isEnter = reflect.set(__obj__, 'name', 'Lucy')
    console.log("_isEnter --->", _isEnter)


    const __name__ = reflect.get(__obj__, 'name')
    console.log("__name__ --->", __name__)


    console.log("__obj__ --->", __obj__)

  </script>
</body>

</html>